package com.kingland.springbootstartflyway.config;

import org.flywaydb.core.Flyway;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
@Configuration

public class FlywayConfig {

    /**
     * Flyway migration bean
     */
    @Bean
    public CommandLineRunner flyway(DataSource dataSource) {
        return args -> {
            Flyway flywaySchema = Flyway.configure()
                    .schemas("public")
                    .dataSource(dataSource)
                    .locations("db/migration/schema")
                    .table("schema_version")
                    .outOfOrder(false)
                    .baselineOnMigrate(true).load();
            flywaySchema.migrate();
        };
    }
}
